package views;
/**
 * EDCAddDTDViewer
 * Viewer giving the ability to add a DTD in current folder from a File or an URL
 */

import io.FileControler;
import io.XMLControler;

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.beans.PropertyVetoException;
import java.io.File;
import java.io.IOException;
import java.util.StringTokenizer;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.filechooser.FileFilter;

import ui.EDC;
import utils.EDCResource;
import utils.EDCUtils;

public class EDCAddDTDViewer extends EDCDefaultViewer implements ActionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private String urlStr = "URL";
	private String localStr = "Local File";

	private JLabel lblAddFrom;
	private JTextField txtURL;
	private JTextField txtLocal;

//	private JButton btURL;
	private JButton btLocal;

	private JRadioButton urlButton;
	private JRadioButton fileButton;

	private JButton btCancel;
	private JButton btOk;

	private JFileChooser fc;

	public EDCAddDTDViewer() {

		setTitle("Add DTD from...");
		
		getContentPane().setLayout(new BorderLayout());
		
		GridBagLayout gridbag = new GridBagLayout();
		GridBagConstraints c = new GridBagConstraints();

		JPanel panel = new JPanel();
		panel.setLayout(gridbag);
		getContentPane().add(panel, BorderLayout.CENTER);

		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 0;
		c.gridy = 0;

		lblAddFrom = new JLabel("Add DTD from...");
		panel.add(lblAddFrom, c);

		c.gridx = 0;
		c.gridy = 1;

		urlButton = new JRadioButton(urlStr);
		urlButton.setMnemonic(KeyEvent.VK_B);
		urlButton.setActionCommand(urlStr);
		urlButton.addActionListener(this);
		panel.add(urlButton, c);

		c.gridx = 1;
		c.gridy = 1;

		txtURL = new JTextField(20);
		txtURL.setEnabled(false);
		txtURL.addActionListener(this);
		panel.add(txtURL, c);

//		c.gridx = 2;
//		c.gridy = 1;

//		btURL = new JButton("URL");
//		btURL.setEnabled(false);
//		btURL.addActionListener(this);
//		panel.add(btURL, c);

		c.gridx = 0;
		c.gridy = 2;

		fileButton = new JRadioButton(localStr);
		fileButton.setMnemonic(KeyEvent.VK_C);
		fileButton.setActionCommand(localStr);
		fileButton.addActionListener(this);
		panel.add(fileButton, c);

		//Group the radio buttons.
		ButtonGroup group = new ButtonGroup();
		group.add(urlButton);
		group.add(fileButton);

		c.gridx = 1;
		c.gridy = 2;

		txtLocal = new JTextField(20);
		txtLocal.setEnabled(false);
		txtLocal.addActionListener(this);
		panel.add(txtLocal, c);

		c.gridx = 2;
		c.gridy = 2;

		btLocal = new JButton("File");
		btLocal.setEnabled(false);
		btLocal.addActionListener(this);
		panel.add(btLocal, c);
		
		JPanel okPane = new JPanel();

		btCancel = new JButton("Cancel");
		btCancel.addActionListener(this);
		okPane.add(btCancel);

		btOk = new JButton("OK");
		btOk.addActionListener(this);
		okPane.add(btOk);
		
		getContentPane().add(okPane, BorderLayout.PAGE_END);

	}

	public void actionPerformed(ActionEvent e) {
		Object src = e.getSource();
		if(src instanceof JButton) {
			JButton b = (JButton)src;
			if(b.getText().equals("OK")) {
				System.out.println(b.getText());
				if(fileButton.isSelected()) {

					//Copy the DTD file to the specified directory
					File file = fc.getSelectedFile();
					String fileName = file.getName();
					String path1 = txtLocal.getText();
//					String path2 = EDCResource.USER_DIR+File.separator
//					+EDCResource.dtdFolderName+File.separator+fileName;
					String path2 = "";
					try {
						path2 = EDCResource.inputFile.getCanonicalPath();
					} catch (Exception e2) {
						e2.printStackTrace();
					}
					System.out.println("Path1 = "+path1);
					System.out.println("Path2 = "+path2);

					//Add in dtd directory
					FileControler.copy(path1,path2);

					//Add in urlcatalog.xml
					XMLControler.addDTDNode(fileName, path1);
					
					EDC.getCurrent().refreshNavigator();
					
				}
				if(urlButton.isSelected()) {
					// download file to dtd directory
					// add in urlcatalog.xml

					//Download the DTD file of the url to the specified directory
					String path1 = txtURL.getText();
					String fileName = null;
					// Get only file name
					StringTokenizer st=new StringTokenizer(path1, "/");
					while (st.hasMoreTokens()) {
						fileName=st.nextToken();
					}
//					String path2 = EDCResource.USER_DIR+File.separator
//					+EDCResource.dtdFolderName+File.separator+fileName;
					String path2 = "";
					try {
						path2 = EDCResource.inputFile.getCanonicalPath();
					} catch (Exception e2) {
						e2.printStackTrace();
					}
					System.out.println("Path1 = "+path1);
					System.out.println("Path2 = "+path2);

					//System.out.println("Nom de fichier : "+file.getName());
					FileControler.copyURLFile(path1,path2);
					
					//Add in urlcatalog.xml
					XMLControler.addDTDNode(fileName, path1);
					
					EDC.getCurrent().refreshNavigator();

				}
				
				//Close internal frame
				try {
					setClosed(true);
				} catch (PropertyVetoException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
			}
			else if(b.getText().equals("Cancel")) {
				System.out.println(b.getText());
				EDCResource.ADD_DTD_REQUIRED = false;
				//Close internal frame
				try {
					setClosed(true);
				} catch (PropertyVetoException e1) {
					e1.printStackTrace();
				}
			}
			else if(b.getText().equals("URL")) {
				System.out.println(b.getText());
			}
			else if(b.getText().equals("File")) {
				System.out.println(b.getText());
				if (fc == null) {
					fc = new JFileChooser();
					fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
					fc.setCurrentDirectory(new File(EDCResource.USER_DIR));
					fc.addChoosableFileFilter(new DTDFilter());
				}

				//Show it.
				int returnVal = fc.showDialog(EDCAddDTDViewer.this,
				"Choose DTD File");

				//Process the results.
				if (returnVal == JFileChooser.APPROVE_OPTION) {
					File file = fc.getSelectedFile();
					try {
						txtLocal.setText(file.getCanonicalPath());
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
			}
		}
		if(src instanceof JRadioButton) {
			JRadioButton b = (JRadioButton)src;
			if(b.getText().equals(urlStr)) {
				txtURL.setEnabled(true);
//				btURL.setEnabled(true);
				txtLocal.setEnabled(false);
				btLocal.setEnabled(false);
			}
			else if(b.getText().equals(localStr)) {
				txtURL.setEnabled(false);
//				btURL.setEnabled(false);
				txtLocal.setEnabled(true);
				btLocal.setEnabled(true);
			}
		}

	}
	
	

	protected class DTDFilter extends FileFilter {

		//Accept all dtd, ent, and mod files.
		public boolean accept(File f) {
			if (f.isDirectory()) {
				return true;
			}

			String extension = EDCUtils.getExtension(f);
			if (extension != null) {
				if (extension.toLowerCase().equals("dtd") ||
						extension.toLowerCase().equals("ent") ||
						extension.toLowerCase().equals("mod")) {
					return true;
				} else {
					return false;
				}
			}

			return false;
		}

		//The description of this filter
		public String getDescription() {
			return "*.dtd, *.ent, *.mod";
		}
	}
}
